package values.item
{
import core.Configuration;

import flash.utils.Dictionary;

import framework.dao.EntryUtil;

import values.building.FinalBuildFriendCost;
import values.common.CostValue;
import values.item.building.BuildingUpgradeRequirementValue;

/**
 * BuildingItemTemplateValue
 * @author jliang
 *
 */
public class BuildingItemTemplateValue extends UpgradebleItemTemplateValue
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function BuildingItemTemplateValue()
	{
		super();
	}
	//==========================================================================
	//  Overridden methods: ItemTemplateValue
	//==========================================================================
	/**
	 * <params>
		<width>1</width>
		<height>1</height>
		<walkable>false</walkable>
		<asset_url>building/building_1.swf</asset_url>
		<asset_classRef>Building_1</asset_url>
		<asset_default_direction>0</asset_default_direction>
		<build_count>3</build_count>
		<build_cost>
		  <item key="coin" num="100"/>
		</build_cost>
		<final_build_cost special="true">
			<friend_cost>
				<position index="1" name="@building_1_friend_position_1_name" cash_cost="3" />
				<position index="2" name="@building_1_friend_position_2_name" cash_cost="3" />
				<position index="3" name="@building_1_friend_position_3_name" cash_cost="3" />
			</friend_cost>
			<resource_cost>
			  <item key="mock_resource_1" num="5"/>
			  <item key="mock_resource_2" num="5"/>
			</resource_cost>
		</final_build_cost>
      </params> 
	 */		
	[Data(type="field", mapping="params")]
	override public function set params(value:*):void
	{
		super.params = value;
	}
	
	override public function get dynamicClassRef():Class
	{
		return BuildingItemValue;
	}
	//==========================================================================
	//  Properties
	//==========================================================================	
	public function get width():int
	{
		return int(params.width);
	}
	
	public function get height():int
	{
		return int(params.height);
	}
	
	public function get walkable():Boolean
	{
		return String(params.walkable) == "true" ? true : false;
	}
	
	public function get assetUrl():String
	{
		return Configuration.assetsPath + String(params.asset_url);
	}
	
	public function get assetClassRef():String
	{
		return String(params.asset_classRef);
	}
	
	public function get assetDefaultDirection():int
	{
		return int(params.asset_default_direction);
	}
	
	public function get buildCount():int
	{
		return int(params.build_count);
	}
	
	public function get needToConstruct():Boolean
	{
		return buildCount > 0;
	}
	
	protected var _buildCost:CostValue;
	
	public function get buildCost():CostValue
	{
		if (!_buildCost)
		{
			_buildCost = EntryUtil.mapping(params.build_cost, CostValue);
		}
		return _buildCost;
	}
	
	protected var _finalBuildResourceCost:CostValue;
	
	public function get finalBuildResourceCost():CostValue
	{
		if (!_finalBuildResourceCost)
		{
			_finalBuildResourceCost = EntryUtil.mapping(params.final_build_cost.resource_cost, CostValue);
		}
		return _finalBuildResourceCost;
	}
	
	protected var _finalBuildFriendCost:FinalBuildFriendCost;
	
	public function get finalBuildFriendCost():FinalBuildFriendCost
	{
		if (!_finalBuildFriendCost)
		{
			_finalBuildFriendCost = EntryUtil.mapping(params.final_build_cost.friend_cost, FinalBuildFriendCost);
		}
		return _finalBuildFriendCost;
	}
	
	public function get isSpecialFinaBuildCost():Boolean
	{
		return String(params.final_build_cost.@special) == "true";
	}
	
	protected var _states:Dictionary;
	
	public function get states():Dictionary
	{
		return _states;
	}
	
	[Data(type="mapping", 
    		  mapping="states.state", 
    		  ref="values.item.BuildingStateValue",
    		  way="one_to_more",
    		  object="flash.utils.Dictionary",
              key="@key")]
	public function set states(value:Dictionary):void
	{
		_states = value;
	}
	
	public function getStateByKey(key:int):BuildingStateValue
	{
		return states[key];
	}
	
	protected var upgradeRequirements:Dictionary = new Dictionary(true);
	
	public function getUpgradeRequirementByLevel(lvl:int):BuildingUpgradeRequirementValue
	{
		if (!upgradeRequirements[lvl])
		{
			upgradeRequirements[lvl] = EntryUtil.mapping(getUpgradeByLevel(lvl).requirements, BuildingUpgradeRequirementValue);
		}
		return upgradeRequirements[lvl];
	}
}
}





